package com.treetao.union.web.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.treetao.union.core.util.system.SystemInfoUtil;
import com.treetao.union.db.model.vo.RestResponse;
import com.treetao.union.db.service.CommonUserService;
import com.treetao.union.db.service.MallOrderService;
import com.treetao.union.web.constant.Constants;

/**
 * 看板
 * 
 * @author devm
 *
 */
@RestController
@RequestMapping(Constants.AMIND_PREFIX + "/board")
public class BoardController {
	@Autowired
	private MallOrderService orderService;
	@Autowired
	private CommonUserService userService;

	@GetMapping("/server")
	public RestResponse server() {
		return RestResponse.success().setData(SystemInfoUtil.getAll());
	}

	// 统计数据
	@GetMapping("/nums")
	public RestResponse nums() {
		// 今日订单数
		int todayordernum = orderService.getTodayOrderNum();
		// 本月订单数
		int monthordernum = orderService.getMonthOrderNum();
		// 今日销售额
		double todaymoney = orderService.getTodayMoney();
		// 本月销售额
		double monthmoney = orderService.getMonthMoney();
		// 今日新增用户
		int todayusernum = userService.getTodayNewuserNum();
		// 总数
		int totalusernum = userService.getTotalUserNum();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("todayordernum", todayordernum);
		map.put("monthordernum", monthordernum);
		map.put("todaymoney", todaymoney);
		map.put("monthmoney", monthmoney);
		map.put("todayusernum", todayusernum);
		map.put("totalusernum", totalusernum);
		return RestResponse.success(map);
	}

	// 销售图表数据
	@GetMapping("/chartMoney/{type}")
	public RestResponse chartMoney(@PathVariable("type") String type) {
		Map<String, Object> result = Maps.newHashMap();
		if ("year".equals(type)) {
			String[] months = getMonths();
			List<Double> ydatas = Lists.newArrayList();
			for (String monthStr : months) {
				ydatas.add(orderService.getMonthMoney(monthStr));
			}
			result.put("xdatas", months);
			result.put("ydatas", ydatas);
		} else if ("month".equals(type)) {
			List<String> days = getDayListOfMonth();
			List<Double> ydatas = Lists.newArrayList();
			for (String dayStr : days) {
				ydatas.add(orderService.getDayMoney(dayStr));
			}
			result.put("xdatas", days);
			result.put("ydatas", ydatas);
		}
		return RestResponse.success(result);
	}

	private String[] getMonths() {
		Calendar cal = Calendar.getInstance(Locale.CHINA);
		cal.setTime(new Date());
		int year = cal.get(Calendar.YEAR);
		return new String[] { year + "-01", year + "-02", year + "-03", year + "-04", year + "-05", year + "-06",
				year + "-07", year + "-08", year + "-09", year + "-10", year + "-11", year + "-12" };
	}

	/**
	 * 获取当月所有天
	 * 
	 * @return
	 */
	public static List<String> getDayListOfMonth() {
		List<String> list = new ArrayList<String>();
		Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
		int year = aCalendar.get(Calendar.YEAR);// 年份
		int month = aCalendar.get(Calendar.MONTH) + 1;// 月份
		int day = aCalendar.getActualMaximum(Calendar.DATE);
		for (int i = 1; i <= day; i++) {
			String aDate = String.valueOf(year) + "-" + (month>9?month:("0"+month)) + "-" + (i>9?i:("0"+i));
			list.add(aDate);
		}
		return list;
	}
}
